package mmi.gui.widget;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import mmi.gui.MainFrame;

/**
 * 
 * @author louis
 *
 */
public class LineWidthChooser extends JFrame implements ActionListener {
	private static final long serialVersionUID = -9105729759086515175L;

	private final MainFrame parent;

	JTextField tf = null;

	JButton okButton = null;

	JButton cancelButton = null;
	public LineWidthChooser(MainFrame parent) {
		super();
		this.parent = parent;
		initialise();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if("Ok".equals(e.getActionCommand()))
		{
			try
			{
				parent.getDrawingArea().setLineWidth(Integer.parseInt(tf.getText()));
				dispose();
			} catch(NumberFormatException exc)
			{
				tf.setText(""+parent.getDrawingArea().getLineWidth());
			}
		} else if("Cancel".equals(e.getActionCommand()))
		{
			dispose();
		}

	}

	private void initialise()
	{
		setSize(800, 500);
		setName("mainFrame");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Drawing tool");


		this.setLayout(new BorderLayout());
		tf = new JTextField();
		add(tf, BorderLayout.NORTH);
		tf.setText(""+parent.getDrawingArea().getLineWidth());

		okButton = new JButton("Ok");
		okButton.setActionCommand("Ok");
		okButton.addActionListener(this);
		add(okButton, BorderLayout.EAST);

		cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(this);
		add(cancelButton, BorderLayout.WEST);

		pack();
		setVisible(true);
	}

}
